// 引入封装的axios方法
import { reqCategoryList, reqGetbannerList, reqGetfloorList } from '@/api';

// home模块的小仓库
const state = {
  // state中数据默认初始值根据服务器端返回的数据类型设置, (如果返回的是数组那就设默认值为数组,...)
  categoryList: [],

  // 轮播图数据
  bannerList: [],

  // floor轮播图数据
  floorList: [],
};

const actions = {
  // 通过API里面的接口函数调用, 向服务器发请求, 获取服务器的数据
  async categoryList(context) {
    // 获取数据
    const result = await reqCategoryList();
    // 获取数据成功后, 调用mutations中的方法
    if (result.code === 200) context.commit('CATEGORYLIST', result.data);
  },

  // 获取mock 轮播图数据
  async bannerList(context) {
    const result = await reqGetbannerList();

    if (result.code === 200) context.commit('BANNERLIST', result.data);
  },

  // 获取mock floor数据
  async floorList(context) {
    const result = await reqGetfloorList();

    if (result.code === 200) context.commit('FLOORLIST', result.data);
  },
};

const mutations = {
  CATEGORYLIST(state, categoryList) {
    // 将数据传递到state中
    state.categoryList = categoryList;
  },

  BANNERLIST(state, bannerList) {
    state.bannerList = bannerList;
  },

  FLOORLIST(state, floorList) {
    state.floorList = floorList;
  },
};
const getters = {};

export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters,
};
